﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutomatedTestDataManagement.Models;

namespace AutomatedTestDataManagement.Controllers
{
    public class ScenariosTestController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /ScenariosTest/

        public ActionResult Index()
        {
            var testscenariotests = db.TestScenarioTests.Include(t => t.Test).Include(t => t.TestScenario);
            return View(testscenariotests.ToList());
        }

        //
        // GET: /ScenariosTest/Details/5

        public ActionResult Details(int id = 0)
        {
            TestScenarioTest testscenariotest = db.TestScenarioTests.Find(id);
            if (testscenariotest == null)
            {
                return HttpNotFound();
            }
            return View(testscenariotest);
        }

        //
        // GET: /ScenariosTest/Create

        public ActionResult Create(int id = 0, int suiteID = 0)
        {
            if (id == 0)
            {
                ViewBag.ScenarioID = new SelectList(db.TestScenarios, "ScenarioID", "ScenarioName");
                ViewBag.TestNumber = 0;
            }
            else
            {
                ViewBag.ScenarioID = new SelectList(db.TestScenarios, "ScenarioID", "ScenarioName", id);
                int maxNum = 0;
                if (db.TestScenarioTests.Where(tst => tst.ScenarioID == id).Count() != 0)
                {
                    maxNum = db.TestScenarioTests.Where(tst => tst.ScenarioID == id).Max(tst => tst.TestNumber ?? 0);
                }
                ViewBag.TestNumber = maxNum + 1;
            }
            if (suiteID != 0)
            {
                ViewBag.SuiteID = suiteID;
            }

            ViewBag.SelectedScenarioID = id;
            ViewBag.TestID = new SelectList(db.Tests, "TestID", "TestName");
            return View();
        }

        //
        // POST: /ScenariosTest/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(TestScenarioTest testscenariotest)
        {
            if (ModelState.IsValid)
            {
                db.TestScenarioTests.Add(testscenariotest);
                db.SaveChanges();
                if (Request.Form["SuiteID"] != null)
                {
                    return RedirectToAction("Edit", "TestScenario", new { id = testscenariotest.ScenarioID, suiteID = Request.Form["SuiteID"]});
                }
                else
                {
                    return RedirectToAction("Edit", "TestScenario", new { id = testscenariotest.ScenarioID });
                }
            }

            ViewBag.TestID = new SelectList(db.Tests, "TestID", "TestName", testscenariotest.TestID);
            ViewBag.ScenarioID = new SelectList(db.TestScenarios, "ScenarioID", "ScenarioName", testscenariotest.ScenarioID);
            return View(testscenariotest);
        }

        //
        // GET: /ScenariosTest/Edit/5

        public ActionResult Edit(int id = 0, int suiteID = 0)
        {
            TestScenarioTest testscenariotest = db.TestScenarioTests.Find(id);
            if (testscenariotest == null)
            {
                return HttpNotFound();
            }
            if (suiteID != null)
            {
                ViewBag.SuiteID = suiteID;
            }

            ViewBag.TestID = new SelectList(db.Tests, "TestID", "TestName", testscenariotest.TestID);
            ViewBag.ScenarioID = new SelectList(db.TestScenarios, "ScenarioID", "ScenarioName", testscenariotest.ScenarioID);
            return View(testscenariotest);
        }

        //
        // POST: /ScenariosTest/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(TestScenarioTest testscenariotest)
        {
            if (ModelState.IsValid)
            {
                db.Entry(testscenariotest).State = EntityState.Modified;
                db.SaveChanges();

                if (Request.Form["SuiteID"] != null)
                {
                    return RedirectToAction("Edit", "TestScenario", new { id = testscenariotest.ScenarioID, suiteID = Request.Form["SuiteID"] });
                }
                else
                {
                    return RedirectToAction("Edit", "TestScenario", new { id = testscenariotest.ScenarioID });
                }

            }
            ViewBag.TestID = new SelectList(db.Tests, "TestID", "TestName", testscenariotest.TestID);
            ViewBag.ScenarioID = new SelectList(db.TestScenarios, "ScenarioID", "ScenarioName", testscenariotest.ScenarioID);
            return View(testscenariotest);
        }

        //
        // GET: /ScenariosTest/Delete/5

        public ActionResult Delete(int id = 0, int scenarioID = 0, int suiteID = 0, int testID = 0, string returnToAction = "Edit")
        {
            TestScenarioTest testscenariotest = db.TestScenarioTests.Find(id);
            if (testscenariotest == null)
            {
                return HttpNotFound();
            }
            if (scenarioID != 0) { ViewBag.ScenarioID = scenarioID; }
            if (suiteID != 0) { ViewBag.SuiteID = suiteID; }
            if (testID != 0) { ViewBag.TestID = testID; }
            ViewBag.ReturnToAction = returnToAction;
            return View(testscenariotest);
        }

        //
        // POST: /ScenariosTest/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var suiteID = Request.Form["SuiteID"];
            var scenarioID = Request.Form["ScenarioID"];
            var testID = Request.Form["TestID"];
            var returnToAction = Request.Form["ReturnToAction"];

            TestScenarioTest testscenariotest = db.TestScenarioTests.Find(id);
            db.TestScenarioTests.Remove(testscenariotest);
            db.SaveChanges();

            if (scenarioID != null)
            {
                if (suiteID != null)
                {
                    return RedirectToAction(returnToAction, "TestScenario", new { id = scenarioID, suiteID = suiteID });
                }
                else
                {
                    return RedirectToAction(returnToAction, "TestScenario", new { id = scenarioID });
                }
            }
            else
            {
                if (testID != null)
                {
                    return RedirectToAction("Delete", "Test", new { id = testID });
                }
                else
                {
                    return RedirectToAction("Index", "TestScenario");
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}